<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<meta charset="UTF-8" />
<title>斗地主测试</title>
<script src="/socket.io.js"></script>
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
<script>
    $(function(){
        let userId,currentRoomId,gameType =1001;
        socket = io("123.57.129.108:3080");
        socket.on('connect', function(){
            console.log('connect a socket client');
        });
        $("#send").click(function(){
            userId = $("#userId").val();
            socket.emit("open", {userId})
        })
        //出牌
        $("#chupaiBtn").click(function(){
            let poker = $("#poker").val();
            socket.emit("game_chupai", {userId, roomId:currentRoomId,pais:[+poker],gameType})
        })
        //加倍请求
        $("#multiBtn").click(function(){
            socket.emit("game_double", {userId, roomId:currentRoomId,gameType,multi:3})
        })
        //同意加倍
        $("#agreeBtn").click(function(){
            socket.emit("game_agree_double", {userId, roomId:currentRoomId,gameType})
        })
        //放弃
        $("#giveupBtn").click(function(){
            socket.emit("game_giveup", {userId, roomId:currentRoomId,gameType})
        })
        socket.on("data", function(eventList){
            console.log("=================")
            for(let event of eventList){
                let {data,eventName} = event;
                data = data || {};
                let {code, msg} = data;
                console.log("eventName:"+eventName+",code:"+code+",msg:"+msg)
                console.log(data)
                if(eventName == "open_result"){
                    let {gameId, roomId,index} = data || {};
                    if(!roomId){
                        socket.emit("room_match")
                    }else{
                        socket.emit("room_enter", {roomId})
                    }
                }
                if(eventName == "room_match_result"){
                    let {roomId} = data;
                    currentRoomId = roomId;
                    socket.emit("room_enter", {roomId})
                }
                if(eventName == "room_enter_result"){
                    let {roomId,seats} = data;
                    currentRoomId = roomId;
                    let indexs = [0,1];
                    for(let {index} of seats){
                        let arrIndex = indexs.indexOf(index);
                        indexs.splice(arrIndex,1)
                    }
                    console.log("--------------")
                    console.log(indexs)
                    socket.emit("room_sitdown", {roomId, index:indexs[0]})
                }
                if(eventName == "room_sitdown_push"){
                }
                if(eventName == "begin_push"){
                }
            }
        })
        //其他事件
        // socket.on("begin_push", function(result){
        //     console.log("begin_push")
        //     console.log(result)
        // })
        // socket.on("deal_poker_push", function(result){
        //     console.log("deal_poker_push")
        //     console.log(result)
        // })
        // socket.on("operate_push", function(result){
        //     console.log("operate_push")
        //     console.log(result)
        // })
        // socket.on("chupai_push", function(result){
        //     console.log("chupai_push")
        //     console.log(result)
        // })
        // socket.on("game_over_push", function(result){
        //     console.log("game_over_push")
        //     console.log(result)
        // })
        // socket.on("game_result", function(result){
        //     console.log("game_result")
        //     console.log(result)
        // })
        // socket.on("operate_push", function(result){
        //     console.log("operate_push")
        //     console.log(result)
        // })
    })
</script>
</head>
<body>
    <input type="text" id="userId" value="1001"><input type="button" value="open" id="send"></br>
    <input type="text" id="poker" value=""><input type="button" value="出牌" id="chupaiBtn"></br>
    <input type="button" value="加倍请求" id="multiBtn"></br>
    <input type="button" value="同意加倍" id="agreeBtn"></br>
    <input type="button" value="放弃" id="giveupBtn"></br>
    <br/>
    <br/>
    <textarea style="width: 400px;height:200px;">

    </textarea>
</body>
</html>